package org.linlinjava.litemall.admin.service;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.metadata.ReadSheet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.admin.dto.LiquorLibraryExcelData;
import org.linlinjava.litemall.admin.dto.LiquorLibraryListener;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.IliquormallLibrary;
import org.linlinjava.litemall.db.domain.IliquormallTopic;
import org.linlinjava.litemall.db.service.ILiquormallLibraryService;
import org.linlinjava.litemall.db.service.ILiquormallTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminLiquorLibraryService {
    private final Log logger = LogFactory.getLog(AdminLiquorLibraryService.class);

    @Autowired
    private ILiquormallLibraryService liquorLibraryService;

    public Object list(String name, String province, String type, String brand, Integer page, Integer limit) {
        List<IliquormallLibrary> liquorLibraryList = liquorLibraryService.queryLiquorLibraryList(name, province, type, brand, page, limit);
        return ResponseUtil.okList(liquorLibraryList);
    }

    /**
     * 校验酒业咨询合法性
     *
     * @param liquorLibrary
     * @return
     */
    private Object validate(IliquormallLibrary liquorLibrary) {
//        if (StringUtils.isEmpty(liquorLibrary.getTitle())) {
//            return ResponseUtil.badArgument();
//        }

        return null;
    }

    /**
     * 删除酒库
     *
     * @param liquorLibrary
     * @return
     */
    @Transactional
    public Object delete(IliquormallLibrary liquorLibrary) {
        Integer id = liquorLibrary.getId();

        if (id == null) {
            return ResponseUtil.badArgument();
        }

        if (liquorLibraryService.deleteLiquorLibrary(id) == 0) {
            return ResponseUtil.fail();
        }

        return ResponseUtil.ok();

    }

    /**
     * 添加酒库
     *
     * @param liquorLibrary
     * @return
     */
    @Transactional
    public Object create(IliquormallLibrary liquorLibrary) {
        Object error = validate(liquorLibrary);

        if (error != null) {
            return error;
        }

        liquorLibrary.setDelete(Byte.valueOf("0"));

        if (liquorLibraryService.insertLiquorLibrary(liquorLibrary) == 0) {
            return ResponseUtil.fail();
        }

        return ResponseUtil.ok();
    }

    /**
     * 更新酒业资讯
     *
     * @param liquorLibrary
     * @return
     */
    @Transactional
    public Object update(IliquormallLibrary liquorLibrary) {
        if (liquorLibraryService.updateLiquorLibrary(liquorLibrary) == 0) {
            return ResponseUtil.fail();
        }

        return ResponseUtil.ok();
    }

    /**
     * 返回酒业资讯详情
     *
     * @param id
     * @return
     */
    public Object detail(Integer id) {

        IliquormallLibrary liquorLibrary = liquorLibraryService.selectById(id);

        Map<String, Object> data = new HashMap<>();
        data.put("liquorLibrary", liquorLibrary);

        return ResponseUtil.ok(data);
    }

    public Object batchUpload(InputStream inputStream) {
        LiquorLibraryListener liquorLibraryListener = new LiquorLibraryListener();
        ExcelReader excelReader = EasyExcel.read(inputStream, LiquorLibraryExcelData.class, liquorLibraryListener).build();
        int size = excelReader.excelExecutor().sheetList().size();
        liquorLibraryListener.setLiquormallLibraryService(liquorLibraryService);
        liquorLibraryListener.setTotal(size);
        for (int i = 0; i < size; i++) {
            excelReader.read(EasyExcel.readSheet(i).build());
        }
        excelReader.finish();
        return ResponseUtil.ok();
    }

}
